package com.woniuxy.demo.entity;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class PageBean<T> {

	/*
	 * 当前页的数据集合
	 */
	private List<T> beanList;

	/*
	 * 页索引，当前页码
	 */
	private Integer pageIndex;

	/*
	 * 页大小，每页显示多少条数据
	 */
	private Integer pageSize;

	/*
	 * 总条数
	 */
	private Integer totalRecord;

	/*
	 * 总页数
	 */
	private Integer totalPage;

	/*
	 * 起始页
	 */
	private Integer pageBegin;

	private String pageUrl;

	/*
	 * 结束页
	 */
	private Integer pageEnd;

	public List<T> getBeanList() {
		return beanList;
	}

	public void setBeanList(List<T> beanList) {
		this.beanList = beanList;
	}

	public Integer getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(Integer pageIndex) {
		this.pageIndex = pageIndex;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(Integer totalRecord) {
		this.totalRecord = totalRecord;
	}

	public Integer getTotalPage() {
		return totalRecord % pageSize == 0 ? totalRecord / pageSize : totalRecord / pageSize + 1;
	}

	public Integer getPageBegin() {
		return pageBegin;
	}

	public void setPageBegin(Integer pageBegin) {
		this.pageBegin = pageBegin;
	}

	public Integer getPageEnd() {
		return pageEnd;
	}

	public void setPageEnd(Integer pageEnd) {
		this.pageEnd = pageEnd;
	}

	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public void setPageBeginAndPageEnd() {
		if (getTotalPage() < 10) {
			pageBegin = 1;
			pageEnd = getTotalPage();
		} else {
			pageBegin = pageIndex - 5;
			pageEnd = pageIndex + 4;
			if (pageBegin < 1) {
				pageBegin = 1;
				pageEnd = 10;
			}
			if (pageEnd > getTotalPage()) {
				pageBegin = getTotalPage() - 9;
				pageEnd = getTotalPage();
			}
		}
	}

	/**
	 * 设置请求参数url
	 * 
	 * @return
	 */
	public static String setPageUrl(HttpServletRequest req) {

		String queryString = req.getQueryString();
		int index = queryString.indexOf("&pageIndex");
		if (index != -1) {
			queryString = queryString.substring(0, index);
		}
		return req.getContextPath() + req.getServletPath() + "?" + queryString;
	}
}
